@{
    ViewData["Title"] = "Address book";
    Layout = "~/Views/Shared/_Index.cshtml";
}

<div class="block-content">
    <div class="block-configuration config-bar">
        <a href="#"><i class="fa fa-arrow-circle-right"></i></a>
        <div class="block-configuration-bar">
            <span>列表</span>
            <a class="properties" height="500px" href="javascript: SF.controls.modal.show($(this), '/BlockProperties/175?t=Block Properties')" title="Block Properties"><i class="fa fa-cog"></i></a><a id="ctl00_main_ctl23_ctl01_aSecureBlock" class="security" height="500px" href="javascript: SF.controls.modal.show($(this), '/Secure/9/175?t=Block Security&amp;pb=&amp;sb=Done')" title="Block Security"><i class="fa fa-lock"></i></a><a class="block-move block-move" href="175" data-zone="Main" data-zone-location="Page" title="Move Block"><i class="fa fa-external-link"></i></a>
            <div class="delete block-delete disabled js-disabled">
                <i class="fa fa-times-circle-o"></i>
            </div>
        </div>
    </div>
    <div class="panel panel_no_boder panel-block">
        <div class="panel-heading">
            <div class="pull-left">
                <table>
                    <tr>
                        <td>
                            <input id="txt_Keyword" type="text" class="form-control" placeholder="请输入要查询关键字" style="width: 200px;" />
                        </td>
                        <td style="padding-left: 5px;">
                            <a id="btn_Search" class="btn btn-primary"><i class="fa fa-search"></i>&nbsp;查询</a>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="grid-actions pull-right">
                <a title="新增" class="btn-add btn btn-default btn-sm" onclick="btn_add()"><i class="fa fa-plus-circle"></i></a>
                <a title="编辑" class="btn-add btn btn-default btn-sm" onclick="btn_edit()"><i class="fa  fa-edit"></i></a>
                 <a title="删除" class="btn btn-default" onclick="btn_delete()"><i class="fa fa-trash-o"></i></a>
                <a title="导出" class="btn-excelexport btn btn-default btn-sm"><i class="fa fa-table"></i></a>
            </div>
        </div>
        <div class="panel-body">
            <div class="grid grid-panel">
                <div class="table-responsive">
                    <div class="gridPanel">
                        <table id="gridTable"></table>
                        <div id="gridPager"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        InitialPage();
        GetGrid();
    });
    //初始化页面
    function InitialPage() {

        //resize重设(表格、树形)宽高
        $(window).resize(function (e) {
            window.setTimeout(function () {
                $('#gridTable').setGridWidth(($('.gridPanel').width()));
                $("#gridTable").setGridHeight($(window).height() - 87.5);
                SF.controls.modal.resize($(document).height());
            }, 200);
            e.stopPropagation();
        });
    }

    //加载表格
    function GetGrid() {
        var selectedRowIndex = 0;
        var $gridTable = $("#gridTable");
        $gridTable.jqGrid({
            url: "/Api/DataItem/GetTreeList",
            datatype: "json",
            height: $(window).height() - 87.5,
            autowidth: true,
            colModel: [
                { label: '主键', name: 'Id', hidden: true },
                { label: '名称', name: 'ItemName', index: 'ItemName', width: 200, align: 'left' },
                { label: '编号', name: 'ItemCode', index: 'ItemCode', width: 200, align: 'left' },
                { label: '排序', name: 'Sortindex', index: 'Sortindex', width: 80, align: 'left' },
                {
                    label: "树型", name: "IsTree", index: "IsTree", width: 50, align: "center",
                    formatter: function (cellvalue) {
                        return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                    }
                },
                {
                    label: "有效", name: "EnabledMark", index: "EnabledMark", width: 50, align: "center",
                    formatter: function (cellvalue) {
                        return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                    }
                },
                { label: "备注", name: "Description", index: "Description", width: 200, align: "left" }
            ],
            treeGrid: true,
            treeGridModel: "nested",
            ExpandColumn: "ItemCode",
            rowNum: "10000",
            rownumbers: true,
            onSelectRow: function () {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            }
        });
        //查询事件
        $("#btn_Search").click(function () {
            $gridTable.jqGrid('setGridParam', {
                postData: { keyword: $("#txt_Keyword").val() },
            }).trigger('reloadGrid');
        });
    }

    //新增
    function btn_add() {
        var parentId = $("#gridTable").jqGridRowValue("Id");
        SF.utility.dialogOpen({
            id: "DataItem_Form",
            title: '添加分类',
            url: '/DataItem/Form?parentId=' + parentId,
            width: "1000px",
            height: "500px",

            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick("POST");
            }
        });
    };
    //编辑
    function btn_edit() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        if (SF.utility.checkedRow(keyValue)) {
            SF.utility.dialogOpen({
                id: "DataItem_Form",
                title: '编辑分类',
                url: '/DataItem/Form?keyValue=' + keyValue,
                width: "1000px",
                height: "500px",
                close:true,
                callBack: function (iframeId) {
                    top.frames[iframeId].AcceptClick("PUT");
                }
            });
        }
    }
    //删除
    function btn_delete() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        if (keyValue) {
            SF.utility.removeForm({
                type:"Delete",
                url: "/Api/DataItem/" + keyValue,
               // param: { keyValue: keyValue },
                success: function (data) {
                    $("#gridTable").resetSelection();
                    $("#gridTable").trigger("reloadGrid");
                }
            })
        } else {
            dialogMsg('请选择需要删除的分类！', 0);
        }
    }
</script>